package com.lill.org.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lill.common.component.util.PageUtils;
import com.lill.common.component.util.Query;
import com.lill.common.component.util.R;
import com.lill.org.model.CompanyInfoModel;
import com.lill.org.service.ICompanyInfoService;
import com.lill.org.service.IEmployeeService;
import com.lill.org.service.IPersonService;
import com.lill.support.annotation.CurrentUser;
import com.lill.system.constant.SystemConstant;
import com.lill.system.model.SysUserEntity;

/**
 * 公司管理Controller
 *
 * @author lill
 * @date 2019-04-24 15:25:17
 */
@RestController
@RequestMapping("org/info")
public class CompanyInfoController {
    @Autowired
    private ICompanyInfoService infoService;
    @Autowired
    private IPersonService personService;
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,@CurrentUser SysUserEntity curUser) {
    	if (!SystemConstant.SUPER_ADMIN.equals(curUser.getId())) {
    		params.put("id", curUser.getCompanyId());
    	}
    	Query query = new Query(params);
    	//查询列表数据
    	List<CompanyInfoModel> orgCompanyInfoList = infoService.queryList(query);
    	int total = infoService.queryTotal(query);
    	PageUtils pageUtil = new PageUtils(orgCompanyInfoList, total, query.getLimit(), query.getPage());
    	
    	return R.ok().put("page", pageUtil);
    	
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
        CompanyInfoModel orgCompanyInfo = infoService.queryObject(id);
        return R.ok().put("orgCompanyInfo", orgCompanyInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CompanyInfoModel orgCompanyInfo,@CurrentUser SysUserEntity user) {
    	if(StringUtils.isNotEmpty(orgCompanyInfo.getPhone())&&StringUtils.isNotEmpty(orgCompanyInfo.getAttn())){
    		Map<String, Object> mapQuery=new HashMap<String, Object>();
    		mapQuery.put("phone", orgCompanyInfo.getPhone());
    		List<Map<String, Object>> list = employeeService.queryPersonList(mapQuery);
    		if(null!=list&&!list.isEmpty()){
    			if(orgCompanyInfo.getPhone().equals(list.get(0).get("phone").toString())
    					&&orgCompanyInfo.getAttn().equals(list.get(0).get("name").toString())){
    				orgCompanyInfo.setPersonId(list.get(0).get("id").toString());
    			}else{
    				return R.error(1, "手机号已经被使用！");
    			}
    		}
    	}
    	
    	Map<String, Object> nameQuery=new HashMap<String, Object>();
    	nameQuery.put("nameeq", orgCompanyInfo.getName());
    	int total = infoService.queryTotal(nameQuery);
    	if(total>0){
    		return R.error(1, "公司简称已经被使用！");
    	}
    	
    	Map<String, Object> mapQuery=new HashMap<String, Object>();
    	mapQuery.put("serialNo", orgCompanyInfo.getSerialNo());
    	int num=infoService.queryTotal(mapQuery);
    	if(num>0){
    		return R.error(1, "公司编号已存在！");
    	}
    	orgCompanyInfo.setType(1);
    	orgCompanyInfo.setStatus(1);
    	orgCompanyInfo.setCreateUser(user.getId());
    	orgCompanyInfo.setCreateTime(new Date());
        int result=infoService.save(orgCompanyInfo);
        
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CompanyInfoModel orgCompanyInfo,@CurrentUser SysUserEntity user) {
    	if(null!=orgCompanyInfo.getPhone()&&!"".equals(orgCompanyInfo.getPhone())){
    		Map<String, Object> mapQuery=new HashMap<String, Object>();
    		mapQuery.put("phone", orgCompanyInfo.getPhone());
    		int num=personService.queryTotal(mapQuery);
    		if(num>1){
    			return R.error(1, "手机号已经被使用！");
    		}
    	}
    	
    	Map<String, Object> nameQuery=new HashMap<String, Object>();
    	nameQuery.put("nameeq", orgCompanyInfo.getName());
    	int total = infoService.queryTotal(nameQuery);
    	if(total>1){
    		return R.error(1, "公司简称已经被使用！");
    	}
    	
    	Map<String, Object> mapQuery=new HashMap<String, Object>();
    	mapQuery.put("serialNo", orgCompanyInfo.getSerialNo());
    	mapQuery.put("idNq", orgCompanyInfo.getId());
    	int num=infoService.queryTotal(mapQuery);
    	if(num>0){
    		return R.error(1, "公司编号已存在！");
    	}
    	
    	orgCompanyInfo.setType(1);
    	orgCompanyInfo.setUpdateUser(user.getId());
    	orgCompanyInfo.setUpdateTime(new Date());
        int result=infoService.update(orgCompanyInfo);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids,@CurrentUser SysUserEntity user) {
    	Map<String, Object> params=new HashMap<String, Object>();
    	params.put("ids", ids);
    	params.put("updateUser", user.getId());
    	params.put("updateTime", new Date());
    	
        int result=infoService.deleteLogicByMap(params);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }
    
    /**
     * 冻结公司
     */
    @RequestMapping("/freeze")
    public R freeze(@RequestBody String[] ids,@CurrentUser SysUserEntity user) {
    	Map<String, Object> params=new HashMap<String, Object>();
    	params.put("ids", ids);
    	params.put("updateUser", user.getId());
    	params.put("updateTime", new Date());
    	params.put("status", 0);
    	
    	int result=infoService.freezeLogicByMap(params);
    	if(result>0){
    		return R.ok();
    	}else{
    		return R.failure();
    	}
    }
    
    @RequestMapping("/auditYes")
    public R auditYes(@RequestBody String[] ids,@CurrentUser SysUserEntity user) {
    	Map<String, Object> params=new HashMap<String, Object>();
    	params.put("ids", ids);
    	params.put("updateUser", user.getId());
    	params.put("updateTime", new Date());
    	params.put("status", 1);
    	
    	int result=infoService.freezeLogicByMap(params);
    	if(result>0){
    		return R.ok();
    	}else{
    		return R.failure();
    	}
    }
    @RequestMapping("/auditNo")
    public R auditNo(@RequestBody String[] ids,@CurrentUser SysUserEntity user) {
    	Map<String, Object> params=new HashMap<String, Object>();
    	params.put("ids", ids);
    	params.put("updateUser", user.getId());
    	params.put("updateTime", new Date());
    	params.put("status", 3);
    	
    	int result=infoService.freezeLogicByMap(params);
    	if(result>0){
    		return R.ok();
    	}else{
    		return R.failure();
    	}
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {
        List<CompanyInfoModel> list = infoService.queryList(params);

        return R.ok().put("list", list);
    }
}
